package com.hqyj.project.service.impl;

import com.hqyj.project.dto.AttendanceDto;
import com.hqyj.project.entity.User;
import com.hqyj.project.mapper.AttendMapper;
import com.hqyj.project.mapper.UserMapper;
import com.hqyj.project.service.AttendService;
import com.hqyj.project.utils.CommonResult;
import com.hqyj.project.vo.AttendanceVo;
import com.hqyj.project.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendServiceImpl implements AttendService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AttendMapper attendMapper;

    @Override
    public CommonResult queryAttendance(AttendanceDto attendanceDto) {
        List<AttendanceVo> attendances = attendMapper.selectAttendanceByUserId(attendanceDto);
        Integer totals = attendMapper.selectAttendanceCount(attendanceDto);
        PageVo<AttendanceVo> pageVo=new PageVo(attendanceDto.getCurrentPage(),attendanceDto.getPageSize(),totals,attendances);
        return new CommonResult(200,"success",pageVo);
    }

    @Override
    public CommonResult removeOneByClockId(Integer clockId) {
        attendMapper.deleteOneByClockId(clockId);
        return new CommonResult(200,"删除成功！");
    }

    @Override
    public CommonResult removeAttendances(Integer[] clockIds) {
        attendMapper.deleteAttendances(clockIds);
        return new CommonResult(200,"删除成功！");
    }

    @Override
    public CommonResult updateAttendance(AttendanceDto attendanceDto) {
        attendMapper.updateAttendance(attendanceDto);
        return new CommonResult(200,"修改成功！");
    }

    @Override
    public CommonResult addAttendance(AttendanceDto attendanceDto) {
        User user = userMapper.selectUserByUserName(attendanceDto.getUserName());
        AttendanceDto attendanceDto1 = new AttendanceDto();
        attendanceDto1.setClockDate(attendanceDto.getClockDate());
        attendanceDto1.setMornSignIn(attendanceDto.getMornSignIn());
        attendanceDto1.setMornSignOut(attendanceDto.getMornSignOut());
        attendanceDto1.setAfterSignIn(attendanceDto.getAfterSignIn());
        attendanceDto1.setAfterSignOut(attendanceDto.getAfterSignOut());
        attendanceDto1.setClockUserId(user.getUserId());
        attendMapper.insertAttendance(attendanceDto1);
        return new CommonResult(200,"添加成功！");
    }

    @Override
    public CommonResult querySelf(AttendanceDto attendanceDto) {
        List<AttendanceVo> attendanceVos = attendMapper.selectSelf(attendanceDto);
        return new CommonResult(200,"success",attendanceVos);
    }

}
